package com.frank.oj.service.admin.rejudge.impl;

import com.frank.model.entity.judge.Judge;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.manager.RejudgeManager;
import com.frank.oj.service.admin.rejudge.RejudgeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author frank
 * @Date 2024/4/19
 */
@Service
public class RejudgeServiceImpl implements RejudgeService {
    @Resource
    private RejudgeManager rejudgeManager;

    @Override
    public CommonResult<Judge> rejudge(Long submitId) {
        try {
            Judge judge = rejudgeManager.rejudge(submitId);
            return CommonResult.successResponse(judge, "重判成功，已加入判题队列！");
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Void> rejudgeContestProblem(Long cid, Long pid) {
        try {
            rejudgeManager.rejudgeContestProblem(cid, pid);
            return CommonResult.successResponse("重判成功！该题目对应的全部提交已进入判题队列！");
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Judge> manualJudge(Long submitId, Integer status, Integer score) {
        try {
            return CommonResult.successResponse(rejudgeManager.manualJudge(submitId, status, score));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @Override
    public CommonResult<Judge> cancelJudge(Long submitId) {
        try {
            return CommonResult.successResponse(rejudgeManager.cancelJudge(submitId));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
}
